home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / vgl20.zip / BLITDEMO.C next >
C/C++ Source or Header  |  1993-05-18  |  9KB  |  288 lines

  1. /*****************************************************************************
  2.  BLITDEMO.C
  3.  
  4.  Quick'n'dirty program to show how to use some of the VGL routines.  Just
  5.  bounces some balls around behind three "windows".  Also, a brick wall glides
  6.  back and forth in front of the balls (brick wall from Wolfenstein).  You
  7.  can change the background color in the windows with the LEFT and RIGHT
  8.  arrow keys.  The UP and DOWN arrow keys will increase or decrease the number
  9.  of balls bouncing around.  F1-F3 toggle the display of the three windows
  10.  on and off. Everything is drawn into a virtual screen, then copied to the
  11.  real screen.  The sprites and the demo screen were drawn rather hastily
  12.  in Deluxe Paint.
  13.  
  14.  Mark
  15.  morley@camosun.bc.ca
  16. *****************************************************************************/
  17.  
  18. #include <stdlib.h>
  19. #include "keys.h"
  20. #include "vgl.h"
  21.  
  22. #define MAXSPRITES   128                /* Max # of sprites in this demo    */
  23.  
  24. char far MyBuffer[64000];               /* Our virtual screen               */
  25. char far Palette[768];                  /* Buffer for the palette           */
  26.  
  27. char far RedBall[1000];                 /* Some sprite buffers              */
  28. char far GrnBall[1000];
  29. char far BluBall[1000];
  30. char far OraBall[1000];
  31. char far PurBall[1000];
  32. char far MagBall[1000];
  33. char far CyaBall[1000];
  34. char far YelBall[1000];
  35.  
  36. char far Wall[4096];                    /* Buffer for the wall bitmap       */
  37.  
  38. char far* BitMap[MAXSPRITES];           /* Pointers to bitmaps for each     */
  39.                                         /* sprite                           */
  40. int       BallX[MAXSPRITES];            /* Sprite X positions               */
  41. int       BallY[MAXSPRITES];            /* Sprite Y positions               */
  42. int       BallDX[MAXSPRITES];           /* Sprite X delta                   */
  43. int       BallDY[MAXSPRITES];           /* Sprite Y delta                   */
  44.  
  45.  
  46. Intro()
  47. {
  48.   /* Black the palette */
  49.    vglBlack();
  50.  
  51.    /* Load in a font */
  52.    vglLoadFont( "fonts\\opus.fon" );
  53.  
  54.    /* Turn on bolding */
  55.    vglBoldOn();
  56.  
  57.    /* Set text color to bright green */
  58.    vglTextColor( 10 );
  59.  
  60.    /* Position the "cursor" */
  61.    vglGotoXY( 20, 110 );
  62.  
  63.    /* Display a string */
  64.    vglPuts( "VGL Blitting Demo" );
  65.  
  66.    /* Fade in */
  67.    vglFadeIn( Palette );
  68.  
  69.    /* Delay a bit */
  70.    sleep( 1 );
  71.  
  72.    /* Fade out */
  73.    vglFadeOut( Palette );
  74.  
  75.    /* Clear the screen */
  76.    vglClear( 0 );
  77.  
  78.    /* Set text color to bright yellow */
  79.    vglTextColor( 14 );
  80.  
  81.    /* Position the "cursor" */
  82.    vglGotoXY( 45, 110 );
  83.  
  84.    /* Display some text */
  85.    vglPuts( "By Mark Morley" );
  86.  
  87.    /* Fade in */
  88.    vglFadeIn( Palette );
  89.  
  90.    /* Delay a bit */
  91.    sleep( 1 );
  92.  
  93.    /* Fade out */
  94.    vglFadeOut( Palette );
  95. }
  96.  
  97. main()
  98. {
  99.    int i, x, y, ok = 1, key, wx = 0, wd = 1;
  100.    int win1 = 1, win2 = 1, win3 = 1, back = 0;
  101.    int numsprites = 16;
  102.  
  103.    randomize();
  104.  
  105.    /* Enter mode 13h */
  106.    vglInit();
  107.  
  108.    /* Load the wall image and get the palette as well */
  109.    vglGif( "wall.gif", Wall, Palette, 0, 0 );
  110.  
  111.    /* Load the images for the sprites */
  112.    vglGif( "redball.gif", RedBall, 0, 0, 0 );
  113.    vglGif( "grnball.gif", GrnBall, 0, 0, 0 );
  114.    vglGif( "bluball.gif", BluBall, 0, 0, 0 );
  115.    vglGif( "yelball.gif", YelBall, 0, 0, 0 );
  116.    vglGif( "purball.gif", PurBall, 0, 0, 0 );
  117.    vglGif( "magball.gif", MagBall, 0, 0, 0 );
  118.    vglGif( "oraball.gif", OraBall, 0, 0, 0 );
  119.    vglGif( "cyaball.gif", CyaBall, 0, 0, 0 );
  120.  
  121.    /* Display a little intro */
  122.    Intro();
  123.  
  124.    /* Tell the VGL routines to draw in our virtual screen */
  125.    vglBuffer( MyBuffer );
  126.  
  127.    /* Load the screen image (title, etc) directly into video memory */
  128.    vglGif( "blitdemo.gif", VIDMEM, 0, 0, 0 );
  129.  
  130.    /* Set up some random sprites */
  131.    for( i = 0; i < MAXSPRITES; i++ )
  132.    {
  133.       BallX[i] = random( 320 - 31 );
  134.       BallY[i] = random( 200 - 31 );
  135.       BallDX[i] = 5 - random( 10 );
  136.       BallDY[i] = 5 - random( 10 );
  137.       if( BallDX[i] == 0 )
  138.          BallDX[i] = 1;
  139.       if( BallDY[i] == 0 )
  140.          BallDY[i] = 1;
  141.       switch( i % 8 )
  142.       {
  143.          case 0 : BitMap[i] = RedBall;
  144.                   break;
  145.          case 1 : BitMap[i] = GrnBall;
  146.                   break;
  147.          case 2 : BitMap[i] = BluBall;
  148.                   break;
  149.          case 3 : BitMap[i] = YelBall;
  150.                   break;
  151.          case 4 : BitMap[i] = OraBall;
  152.                   break;
  153.          case 5 : BitMap[i] = CyaBall;
  154.                   break;
  155.          case 6 : BitMap[i] = MagBall;
  156.                   break;
  157.          case 7 : BitMap[i] = PurBall;
  158.                   break;
  159.       }
  160.    }
  161.  
  162.    /* Fade in the palette */
  163.    vglFadeIn( Palette );
  164.  
  165.    /* Loop until ESC is pressed */
  166.    while( ok )
  167.    {
  168.       /* Copy the three windows from our virtual screen into video memory,
  169.          but only if the windows are "active" */
  170.       if( win1 )
  171.          vglCopyW( 12, 32, 45, 136 );    /* Left window */
  172.       if( win2 )
  173.          vglCopyW( 81, 32, 157, 136 );   /* Middle window */
  174.       if( win3 )
  175.          vglCopyW( 262, 32, 45, 136 );   /* Right window */
  176.  
  177.       /* Clear the virtual screen to the background color */
  178.       vglClear( back );
  179.  
  180.       /* Loop through the active sprites... */
  181.       for( i = 0; i < numsprites; i++ )
  182.       {
  183.          /* Draw the sprite into our virtual screen */
  184.          vglPutSprite( BallX[i], BallY[i], 31, 31, BitMap[i] );
  185.  
  186.          /* Update the sprite's position */
  187.          x = BallX[i] + BallDX[i];
  188.          y = BallY[i] + BallDY[i];
  189.          if( x < 0 || x > 320 - 31 )
  190.             BallDX[i] = -BallDX[i];
  191.          else
  192.             BallX[i] = x;
  193.          if( y < 0 || y > 200 - 31 )
  194.             BallDY[i] = -BallDY[i];
  195.          else
  196.             BallY[i] = y;
  197.       }
  198.  
  199.       /* Draw the wall image (not a sprite) */
  200.       vglPut( wx, 68, 64, 64, Wall );
  201.  
  202.       /* Update the wall's position */
  203.       wx += wd;
  204.       if( wx == 256 )
  205.       {
  206.          wd = -1;
  207.          wx--;
  208.       }
  209.       else if( wx == 0 )
  210.       {
  211.          wd = 1;
  212.          wx++;
  213.       }
  214.  
  215.       /* Check for a keypress */
  216.       if( kbhit() )
  217.       {
  218.          if( (key = getch()) == 0 )
  219.             key = 256 * getch();
  220.          switch( key )
  221.          {
  222.             case Escape         : ok = 0;               /* Done! */
  223.                                   break;
  224.             case F1             : win1 = 1 - win1;      /* Toggle window 1 status */
  225.                                   if( !win1 )
  226.                                   {
  227.                                      /* Temporarily draw in video memory */
  228.                                      vglBuffer( VIDMEM );
  229.  
  230.                                      /* Black out the window */
  231.                                      vglBox( 12, 32, 45, 136, 0 );
  232.  
  233.                                      /* Back to the virtual screen */
  234.                                      vglBuffer( MyBuffer );
  235.                                   }
  236.                                   break;
  237.             case F2             : win2 = 1 - win2;
  238.                                   if( !win2 )
  239.                                   {
  240.                                      /* Temporarily draw in video memory */
  241.                                      vglBuffer( VIDMEM );
  242.  
  243.                                      /* Black out the window */
  244.                                      vglBox( 81, 32, 157, 136, 0 );
  245.  
  246.                                      /* Back to the virtual screen */
  247.                                      vglBuffer( MyBuffer );
  248.                                   }
  249.                                   break;
  250.             case F3             : win3 = 1 - win3;
  251.                                   if( !win3 )
  252.                                   {
  253.                                      /* Temporarily draw in video memory */
  254.                                      vglBuffer( VIDMEM );
  255.  
  256.                                      /* Black out the window */
  257.                                      vglBox( 262, 32, 45, 136, 0 );
  258.  
  259.                                      /* Back to the virtual screen */
  260.                                      vglBuffer( MyBuffer );
  261.                                   }
  262.                                   break;
  263.             case Right          : back++;               /* Increase background color */
  264.                                   if( back == 16 )
  265.                                      back = 0;
  266.                                   break;
  267.             case Left           : back--;               /* Decrease background color */
  268.                                   if( back < 0 )
  269.                                      back = 15;
  270.                                   break;
  271.             case Up             : if( numsprites < MAXSPRITES )  /* Increase sprite count */
  272.                                      numsprites++;
  273.                                   break;
  274.             case Down           : if( numsprites > 1 )           /* Decrease sprite count */
  275.                                      numsprites--;
  276.                                   break;
  277.          }
  278.       }
  279.    }
  280.  
  281.    /* Fade out */
  282.    vglFadeOut( Palette );
  283.  
  284.    /* Exit mode 13h */
  285.    vglTerm();
  286.    return;
  287. }
  288.